<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/6
 * Time: 13:11.
 */

namespace app\admin\controller;

use app\service\RankingService;
use app\service\ResourceService;
use think\Db;

/**
 * 排行管理.
 *
 * @author   juzi
 * @blog     https://blog.csdn.net/juziaixiao
 *
 * @version  1.0.0
 * @datetime 2020年7月2日10:49:18
 */
class Ranking extends Common
{
    /**
     * 构造方法.
     *
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     *
     * @version  1.0.0
     * @datetime 2020年6月24日13:23:09
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        // 登录校验
        $this->IsLogin();
    }

    /**
     * 排行列表.
     *
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     *
     * @version  1.0.0
     * @datetime 2020年6月24日13:23:09
     */
    public function Index()
    {
//        $params = input();
//        $params['category'] = empty($params['category']) ? 2: $params['category'];
//
//        $where = RankingService::RankingListWhere($params);
//
//        $where['category'] = $params['category'];
//        // 获取总数
//        $total = RankingService::RankingTotal($where);
//        //列表
//        $info = RankingService::RankingList($where);

        if ('ajax' != input('get.action'))
        {
            return $this->fetch();
        } else {
            // 参数
            $params = input();
            $params['category'] = empty($params['category']) ? 1 : $params['category'];

            $where = RankingService::RankingListWhere($params);

            $where['category'] = $params['category'];
            // 获取总数
            $total = RankingService::RankingTotal($where);
            //列表
            $info = RankingService::RankingList($where);

            $this->assign('params', $params);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $info['data'],'where'=>$where];
        }
    }

    /**
     * 排行头像.
     *
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     *
     * @version  1.0.0
     * @datetime 2020年6月24日13:23:09
     */
    public function GetThumb()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }
        $params = $this->data_get;

        if (intval($params['code']) < 1) {
            $table = 'member';
            $where = 'code="'.$params['code'].'"';
            $field = 'head_portrait thumb';
        } else {
            $table = 'member_property';
            $where = 'id="'.$params['code'].'"';
            $field = 'thumb';
        }

        $re = Db::name($table)->where($where)->field($field)->find();

        if (empty($re['thumb'])) {
            return json(DataReturn('未获取到有效数据', 40001, []));
        }

        $re = ResourceService::AttachmentPathViewHandle($re['thumb'], true);

        return json(DataReturn('获取成功', 0, $re));
    }

    /**
     * 排行编辑页面.
     *
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     *
     * @version 1.0.0
     * @date    2020年7月5日20:17:08
     */
    public function Saveinfo()
    {
        // 参数
        $params = $this->data_get;
        // 数据
        $data = [];
        $table = 1 == $params['category'] ? 'team' : 'member_property';

        if (!empty($params['id'])) {
            $where = RankingService::RankingListWhere($params);
            $where['category'] = $params['category'];
            // 获取列表
            $data_params = [
                'm' => 0,
                'n' => 1,
                'page' => false,
                'where' => [$table.'.id' => intval($params['id'])],
                'field' => $where['field'],
                'category' => $params['category'],
                'table' => $table,
            ];
            $ret = RankingService::RankingList($data_params);
            $data = empty($ret['data'][0]) ? [] : $ret['data'][0];
        }
        $this->assign('res', $data);

        return $this->fetch();
    }

    /**
     * 排行添加/编辑.
     *
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     *
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function Save()
    {
        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }

        // 开始操作
        $params = $this->data_post;
        RankingService::RankingSave($params);

        return json(DataReturn('保存成功', 0));
    }

    /**
     * 删除行业.
     *
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     *
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function DelInfo()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }
        $params = $this->data_post;
        RankingService::DoDel($params);

        return json(DataReturn('删除成功', 0));
    }

    public function aa()
    {
        return $this->fetch();
    }
}
